package com.xidian.GMS.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

import com.xidian.GMS.dto.Page;
import com.xidian.GMS.entity.Announcement;
import com.xidian.GMS.mapper.AnnouncementMapper;
import com.xidian.GMS.service.AnnouncementManage;
import com.xidian.GMS.service.impl.AnnouncementManageImpl;

@Controller
public class AnnouncementController {
@Autowired
  private AnnouncementMapper announcementMapper;
@Autowired
 private AnnouncementManage  announcementManage;
@RequestMapping(value="/announcementEdit",method= RequestMethod.GET)
public String update0(int announcementid ,Model  model){
	    model.addAttribute("announcementid", announcementid);
		return "announcementEdit";
		// TODO: handle exception
	
}


@RequestMapping(value="/announcementEdit",method= RequestMethod.POST)
public String update(String announcementid, String title, String content){
	System.out.println(announcementid);
	int id = Integer.parseInt(announcementid);
	Date edittime = new Date();
		announcementManage.update(id, title, content,edittime);
		return "announcementIndex";
}

@RequestMapping(value="/announcementInsert",method= RequestMethod.POST)
public String Insert(
		String title, String content){
	Date uploadtime = new Date();
	System.out.println(uploadtime);
	System.out.println(title);
	try {		
	    announcementManage.Insert( title, uploadtime, content);
		return "announcementIndex";
	} catch (Exception e) {
		return "announcementInsert";
		// TODO: handle exception
	}
	
}
@RequestMapping(value="/announcementInsert",method= RequestMethod.GET)
public String Insert(){

		return "announcementInsert";
		// TODO: handle exception
	
	
}
@RequestMapping(value="/announcement",method= RequestMethod.GET)
public String Insert22(){

		return "announcement";
		// TODO: handle exception
	
	
}
@RequestMapping(value="/announcementDelete",method= RequestMethod.GET)
public String delete(int announcementid){
	try {
	    announcementManage.delete(announcementid);
		return "forward:/fun4-announcement.html";
	} catch (Exception e) {
		return "announcementInsert";
		// TODO: handle exception
	}
	
}

@RequestMapping(value="/announcementIndex",method= RequestMethod.POST)
public String IndexPost(){

		return "announcementIndex";
		// TODO: handle exception
	}
	

//@RequestMapping(value="/announcementIndex",method= RequestMethod.GET)
//public String InsertGet(){
//
//		return "announcementIndex";
//		// TODO: handle exception
//	
//	
//}

@RequestMapping(value="/announcementIndex", method=RequestMethod.GET)
@ResponseBody
public Page index(@RequestParam(name="cur", defaultValue="1") int cur, Model model){
	 
	Page page = announcementManage.getAnnouncements(cur);
	System.out.println(page);
 	//model.addAttribute("page", page);
	
	return page;
}
	
}
